package eightDataType;

import org.junit.Test;

import java.math.BigDecimal;

/**
 * @author jie
 * @version 创建时间：2018年11月12日 下午5:58:43
 * 类说明
 */
public class FloatDemo {

    //float/int TODO 低精度会自动转为高精度
    @Test
    public void floatDivInt(){
        float f = 1.4f;
        float v = f / 3;
        System.out.println("float/int: 1.4f/3"+v);
    }

    @Test
    public void floatDiv100(){
        float f = 99.9f;
        float v = f / 100;
        System.out.println("float/int: 1.4f/100::"+v);
        System.out.println("float/int: 1.4f/100::"+(9.9f/100));
    }

//    public void static main(String[] args) {
        @Test
        public void main() {


        System.out.println("int 向下取整 向下取整："+(1/2));
        StringBuilder sb = new StringBuilder();
        sb.toString();
        float a = 10;
        System.out.println((a / 3));

        int b = 10;
        System.out.println((double) 0 / 3);


        System.out.println("默认  向下取整:" + (int) (96.9999));// 97 (去掉小数凑整:不管小数是多少，都进一)
        System.out.println("向上取整:" + (int) Math.ceil(96.1));// 97 (去掉小数凑整:不管小数是多少，都进一)
        System.out.println("向下取整" + (int) Math.floor(96.8));// 96 (去掉小数凑整:不论小数是多少，都不进位)
        System.out.println("四舍五入取整:" + Math.round(96.1));// 96 (这个好理解，不解释)
        System.out.println("四舍五入取整:" + Math.round(96.8));// 97

        float[] floats = {1.1f, 2.1f, 3.1f};
        for (int i = 0; i < floats.length; i++) {
            System.out.println("...: " + floats[i]);
        }
    }

    @Test
    public void initNum() {
        String def = "37.3";
        double v = Double.parseDouble(def);
        double v1 = (v - 30) / (50 - 30) * 200;
        System.out.println(v1);
        System.out.println("double-->int " + (int) Math.round(v1));
        float current = getCurrent((int) Math.round(v1));
        System.out.println(current);
    }

    private static float getCurrent(int pro) {
        float current = 30;
        if (pro > 0) {
            float fv = (float) pro / 200;
            current = 30 + fv * (50 - 30);
        }
        BigDecimal b = new BigDecimal(current);
        current = b.setScale(1, BigDecimal.ROUND_HALF_UP).floatValue();
        return current;
    }

}
